//
// Created by user on 2022/3/3.
//
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
static bool cmp(vector<int>& point1, vector<int>& point2) {
    return (point1[0] * point1[0] + point1[1] * point1[1]) < (point2[0] * point2[0] + point2[1] * point2[1]);
}

// 小顶堆
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
    priority_queue<vector<int>, vector<vector<int>>, decltype(&cmp)> pq(cmp);
    for (vector<int> point : points) {
        pq.push(point);
        if (pq.size() > k) pq.pop();
    }
    // 小顶堆中存储的就是出现了前 k 个高频的元素
    vector<vector<int>> res(k, vector<int>(2));
    int index = 0;
    while (!pq.empty()) {
        res[index++] = pq.top();
        pq.pop();
    }
    return res;
}